<?
if(!$cms)
  include("_cms/classes/cms.inc");

  if($cms->user['authorized']) {
    // Store changed values
    if($cms->query->post['update'] && $cms->query->post['id']) {
      $result = $cms->db->update_tuple_array($cms->query->post, "inschrijving_bier", "inschrijving_bier",1);
      if($result->error)
        echo "<div class=\"form_error\">".$result->message."</div>\n";
      else
        echo "<div class=\"form_status\">".$result->message."</div>\n";
    }
    if(isset($cms->query->get['ajax_beer'])) {
      $beer = $cms->db->get_tuple_array('inschrijving_bier', $cms->query->get['ajax_beer']);
      unset($beer['inschrijving']);
      unset($beer['tweede_ronde']);
      unset($beer['derde_ronde']);
      $cms->binary_buffer = json_encode($beer);
      return;
    }


    echo "<script type=\"text/javascript\" src=\"{$cms->styleurl}chosen.jquery.min.js\"></script>\n";
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$cms->styleurl}chosen.css\">\n";
    echo "<h1>Voer beoordelingen van bierkeurmeesters in</h1>";
    echo "<form id=\"jury\" method=\"post\">\n";
    echo "<div class=\"form_box\">\n";
    $sql = "select b.id, b.nummer, t.label from inschrijving_bier b left join biertype t on b.biertype=t.id where punten=0 or punten is null order by biertype, nummer";
    $result = $cms->db->execute($sql);
    // Wedstrijd nr
    $html = "<select id=\"id\" name=\"id\" class=\"required combo\">\n";
    $html .= " <option value=\"\">-Kies</option>\n";
    while($row = $result->get_nextrow())
      $html .= sprintf(" <option value=\"%d\">%s_%02d</option>\n",$row['id'],$row['label'],$row['nummer']);
    $html .= "</select>\n";
    show_field('id', 'Wedstrijd nr.', $html);
    // Keurmeester
    $html = "<select id=\"keurmeester\" name=\"keurmeester\" class=\"required combo\">\n";
    $html .= " <option value=\"\">-Kies</option>\n";
    $sql = "select id, voornaam, achternaam from keurmeester order by achternaam, voornaam";
    $result = $cms->db->execute($sql);
    while($row = $result->get_nextrow())
      $html .= "  <option value=\"$row[id]\">$row[voornaam] $row[achternaam]</option>\n";
    $html .= "</select>\n";
    show_field('keurmeester', 'Keurmeester', $html);
    // Punten
    $html = "<input class=\"required\" id=\"punten\" name=\"punten\" type=\"number\" min=\"0\" max=\"100\">\n";
    show_field('punten', 'Aantal punten', $html);
    // Disdplay a box
    echo "</div>\n";
    echo "<div class=\"form_border\">\n";
    // Visueel
    $html = "<input class=\"required do_sum\" id=\"visueel\" name=\"visueel\" type=\"number\" min=\"0\" max=\"15\"> (max 15 punten)\n";
    show_field('visueel', 'Visuele aspecten', $html);
    // Geur
    $html = "<input class=\"required do_sum\" id=\"geur\" name=\"geur\" type=\"number\" min=\"0\" max=\"25\"> (max 25 punten)\n";
    show_field('geur', 'Geur associaties', $html);
    // Smaak associatie
    $html = "<input class=\"required do_sum\" id=\"smaak_assoc\" name=\"smaak_assoc\" type=\"number\" min=\"0\" max=\"25\"> (max 25 punten)\n";
    show_field('smaak_assoc', 'Smaak associaties', $html);
    // Smaak
    $html = "<input class=\"required do_sum\" id=\"smaak\" name=\"smaak\" type=\"number\" min=\"0\" max=\"35\"> (max 35 punten)\n";
    show_field('smaak', 'Smaak', $html);
    echo "</div>\n";
    // Verzend knop
    $html = "<input type=\"submit\" id=\"update\" name=\"update\" value=\"Invoeren\">\n";
    show_field('update', '&nbsp;', $html);
    echo "</div>\n";
    echo "</form>\n";
  }
  else {
    echo "U dient ingelogd te zijn";
  }
  function show_field($field_name, $title, $field_html) {
    echo "<div class=\"form_field\">\n";
    echo "  <label for=\"$field_name\">$title</label>\n";
    echo "  ".$field_html."\n";
    echo "</div>\n";
  }
?>
<script type="text/javascript">
  $(function() {
    $('select.combo').chosen();
  });
  $(document).ready(function() {
    $('select#id').change( function() {
      $.getJSON("?ajax_beer="+$(this).val(), function(json) {
        for(var propt in json) {
          if(propt && json[propt])
            $('#'+propt).val(json[propt]);
          else
            $('#'+propt).val('');
        }
        $('select.combo').trigger("liszt:updated");
      });
    });
    $('form#jury').submit( function() {
      var form_valid = true;
      $(".required").each(function(){
        if ($.trim($(this).val()).length == 0){
          var name = $(this).parent().children('label').text();
          if(!name || name=='')
            var name = this.id;
          alert("Veld '"+name+"' is verplicht!");
          $(this).focus();
          form_valid = false;
          return false;
        }
      });
      if(!form_valid)
        return form_valid;
      $('input[type="number"]').each(function(){
        var max = parseInt($(this).attr('max'));
        if(parseInt($(this).val()) > max) {
          var name = $(this).parent().children('label').text();
          if(!name || name=='')
            var name = this.id;
          alert("De waarde van '"+name+"' mag niet groter zijn dan "+max+"!");
          form_valid = false;
          $(this).focus();
          return false;
        }
      });
      if(!form_valid)
        return form_valid;
      var total = 0;
      $(".do_sum").each(function(){
        total += parseInt($(this).val());
      });
      if(total != parseInt($('#punten').val())) {
        alert('De som van de onderdelen moet gelijk zijn aan het aantal punten. Corrigeer dit voor op te slaan. Overleg eventueel met de keurmeester!');
        form_valid = false;
        $('#punten').focus();
      }
      return form_valid;
    });
    return true;
  });
</script>

